<template>
  <div class="box">
    <div class="left">
      <div class="title">
        <div class="img">
          <img src="../../assets//login/login-headr.png" alt="" />
        </div>
        <div class="smallTitle">Yan an duo bu Plus</div>
      </div>
      <div class="bigTitle" @click="change">沿岸踱步</div>
      <div>颜值与实例并存，前中后台前端框架一站式解决方案</div>
      <div class="loginImg">
        <img src="../../assets/login/login.png" alt="" />
      </div>
    </div>
    <div class="right">
      <div class="fromBox">
        <div class="headerimg"><img src="../../assets/login/heardImg.png" alt="" /></div>
        <div class="welcome">欢迎来到 Yan An Duo Bu! 👋</div>
        <el-form label-width="0px" :model="loginForm" style="max-width: 460px">
          <el-form-item>
            <el-input v-model="loginForm.name" placeholder="请输入用户名">
              <template #prefix>
                <el-icon><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-input type="password" v-model="loginForm.password" placeholder="请输入密码">
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>

          <div @click="requestTest" class="forget"><a>忘记密码</a></div>

          <el-button type="primary" class="submitcss" @click="onSubmit">登录</el-button>

          <div class="forget">如果你还没有账号？<a>创建账号</a></div>

          <el-divider content-position="left">Rabindranath Tagore</el-divider>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
// 引入 hook
import { useRouter } from "vue-router";
// 初始化一下
const router = useRouter();
import { reactive } from "vue";
import { Lock, User } from "@element-plus/icons-vue"; // 引入图标
import { ElNotification } from "element-plus"; // 引入弹窗信息
import { loginIn } from "@/api/login/index";

// 调用登录接口，传入登录用户名和用户密码，返回用户的token。
// 登录，点击登录，把用户名保存起来，设置一个默认登录成功的token，将
// 这个token存入缓存中，后面调用接口，发现如果有缓存token就可以调用，否则重定向到登录页面
const onSubmit = async () => {
  const res = await loginIn({ name: loginForm.name, password: loginForm.password });
  if (res.code == 200) {
    // 登录成功
    localStorage.setItem("token", res.data.token); // 保存用户的登录token
    localStorage.setItem("userName", loginForm.name); // 保存用户登录的名称
    ElNotification({
      // 登录成功
      title: "登录成功",
      type: "success",
      duration: 1000,
    });

    setTimeout(() => {
      router.push("/");
    }, 1000);
  } else {
    ElNotification({
      // 登录失败
      title: "登录错误",
      type: "error",
    });
  }
};

const loginForm = reactive({
  name: "admin",
  password: "123456",
});

function change() {
  router.push("/");
}

const requestTest = async () => {
  console.log("忘记密码");
};
</script>

<style lang="less" scoped>
.box {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100vh;
  .left {
    flex: 3;
    min-height: 100vh;
    padding: 20px;
    width: 100%;
    background-color: #e9edf7;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    .title {
      display: flex;
      justify-content: center;
      align-items: center;
      margin-bottom: 15px;
      img {
        width: 60px;
        height: 60px;
        margin-right: 10px;
        border-radius: 50px;
      }
      .smallTitle {
        color: #1677ff;
        font-weight: 700;
        font-size: 38px;
      }
    }
    .bigTitle {
      color: #323d6f;
      font-weight: 900;
      font-size: 46px;
      margin-bottom: 20px;
    }
    .loginImg {
      margin-top: 20px;
      border-radius: 4px;
      width: 280px;
      height: 280px;
      overflow: hidden;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
  .right {
    flex: 5;
    min-height: 100vh;
    padding: 20px;
    width: 100%;
    background-color: #f7f7f7;
    display: flex;
    justify-content: center;
    align-items: center;
    .fromBox {
      background-color: #fff;
      width: 60%;
      padding: 60px 0px;
      border-radius: 20px;
      display: flex;
      flex-direction: column;
      justify-content: flex-end;
      align-items: center;
      .headerimg {
        height: 80px;
        width: 80px;
        img {
          height: 100%;
          width: 100%;
          border-radius: 50%;
        }
      }
      .welcome {
        font-weight: bold;
        font-size: 20px;
        margin: 20px 0px;
      }
      .forget {
        text-align: right;
        margin: 15px 0px;
      }
      .submitcss {
        width: 100%;
        border-radius: 50px;
      }
    }
  }
}
</style>
